#取消一些定义的
#.SUFFIXES:.d .o .c



include gcc-gba.mk
include hack_definition.mk
#-------------------------------------------------------------
BUILD		:=	build
LDS			:=	fe8hack.ld
OUTELF		:=	fe8hack.elf
HLDS		:=	hlds.ld
SRCDIR		:=	$(CURDIR)

#==============================================================
#不需要这些
#TEXTS		:=	$(wildcard hack/text/*.t)
#TEXT_HEAD	:=	hack_text.h
#==============================================================

#----------------------------------------------------------------

#----------------------------------------------------------------
#编译器选项
#ARCH		:=	-mcpu=arm7tdmi -mthumb -mthumb-interwork
#
#CFLAGS		:=	-Wall -O2\
#					$(ARCH)

#二进制文件生成选项
OBJCOPYFLAGS	:=	--set-section-flags .rom="r,c,a"

ASFLAGS		:=	$(ARCH)

ASINCLUDE	:=	-I ./asm
				
#LDFLAGS		:=	-T$(LDS) -nostdlib -Wl,-Map,$(OUTELF:.elf=).map
LDFLAGS		+=	-T$(LDS) -nostdlib -Map $(OUTELF:.elf=).map 

#------------------------------------------------------------------
#------------------------------------------------------------------
#目录
#移除除了tools目录的所有
#DIRS 		:=	$(filter-out %tools,$(shell find $(CURDIR) -maxdepth 4 -type d))
DIRS 		:=	$(filter-out %tools,$(shell find . -maxdepth 4 -type d))
#------------------------------------------------------------------
#源文件
#输出文件
CFILES		:=	$(foreach dir,$(DIRS),$(wildcard $(dir)/*.c))
DEPENDS		:=	$(CFILES:.c=.d)
SFILES		:=	$(foreach dir,$(DIRS),$(wildcard $(dir)/*.s))
HACK_DATA_HEAD := $(foreach dir,$(DIRS),$(wildcard $(dir)/*.lds.h))
OBJFILES	:=	$(CFILES:.c=.o)
OBJFILES	+=	$(SFILES:.s=.o)

BUILDDATE	:=	$(shell date '+%Y-%m-%d %H:%M')


#---------------------------------------------------------------------------------
#伪命令
#soul.o 保持每次都编译
.PHONY:all clean build elf $(HLDS) hack_info.o bin clean_hack_texts $(TEXTS:.to=.t) hacklds.h


all:bin

build:$(OBJFILES)
	@echo 编译成功

bin:$(OUTELF)
	$(OBJCOPY) $(OBJCOPYFLAGS) $< 
	$(OBJCOPY) -O binary $< $(OUTELF:.elf=.gba)


elf:$(OUTELF)
	$(OBJCOPY) $(OBJCOPYFLAGS) $< 
	@echo 编译成功


#%.o:%.c
#	$(CC) $(CFLAGS) -c $< -o $@
#-----------------------------------------------------------------------------------
#$(CC) $(CFLAGS) -c -MMD -MF"$*.d" $(CURDIR)/$< -o $@
#上面的指令是先生成*.d依赖项，然后再输出obj文件,  *.d 文件的输出文件名是 -o 的文件名
#头文件更改，会重新编译
-include $(DEPENDS)
#%.o:%.c
#	$(CC) $(CFLAGS) -c -MMD -MF"$*.d" $(CURDIR)/$< -o $@
#-----------------------------------------------------------------------------------
#有关一些数据hack生成链接脚本
$(HLDS):lds.h
	$(CC) -E -P $< -o $@
	
hack_info.o:$(HLDS) $(TEXT_HEAD)
	$(CC) $(CFLAGS) -DBUILDDATE="$(BUILDDATE)" -c $(@:.o=.c) -o $@
	
%.o:%.s
	$(CC) $(CFLAGS) $(ASINCLUDE) -c $< -o $@


$(OUTELF):$(OBJFILES)
	$(LD)  $(LDFLAGS)  $^ -o $@

#=======================================================================================
#一些文件依赖


#--------------------------------------------------------------------------------
#2016-05-06 15:51:45
#文本添加
clean_hack_texts:
	-rm $(TEXT_HEAD)

$(TEXT_HEAD):clean_hack_texts $(TEXTS:.t=.to)

	
%.to:%.t
	@echo \#include \"$(<)\" >> $(TEXT_HEAD)
	
text/text.o:text/text.c
	$(CC) $(CFLAGS) -c -MMD -MF"$*.d" $(CURDIR)/$< -o $@

#=======================================================================================
#链接数据的修改
lds.h:hacklds.h

hacklds.h:$(HACK_DATA_HEAD)
	echo //这个是模块自动创建的链接脚本 > $@
	for h in $(^);do echo \#include \"$$h\" >> $@;done

#========================================================================================

#---------------------------------------------------------------------------------
clean:
	-rm $(OBJFILES)
	-rm $(OUTELF)
	-rm $(DEPENDS)


